import 'package:flutter/material.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/stock_out_child_data_entity.dart';
import 'package:library_base/entities/weighing_goods_child_entity.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/colors.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/button/ink_bg_well.dart';
import 'package:library_base/res/widget_generator.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:module_wms/page/outbound_orders/as_goods/as_goods_controller.dart';
import 'package:library_base/ext/bool_ext.dart';
import 'package:library_base/ext/double_ext.dart';

Widget buildSearchWidget(
    AsGoodsController asOrderController,
    {
      ValueChanged<String>? onChanged,
      ValueChanged<String>? onSearch,
    }
    ) {
  return Container(
    color: ThemeUtil.getTheme().colorScheme.secondary,
    height: 116.w,
    child: Row(
      children: <Widget>[
        Expanded(
          child: Container(
              alignment: Alignment.center,
              height: double.infinity,
              width: double.infinity,
              margin: EdgeInsets.only(left: 24.w, top: 24.w, bottom: 24.w),
              decoration: BoxDecoration(
                color: ThemeUtil.getTheme().secondaryBgColor(),
                borderRadius: const BorderRadius.all(Radius.circular(30.0)),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  gapW24,
                  LocalImage('common_search',
                      package: Constant.packLib(), width: 26.w, height: 26.w),
                  gapW24,
                  Expanded(
                    child: Container(
                      alignment: Alignment.center,
                      height: double.infinity,
                      width: double.infinity,
                      child: TextField(
                        onChanged: onChanged,
                        autofocus: false,
                        style: normalTextColor28,
                        controller: asOrderController.keyWorkSearchController,
                        textAlignVertical: TextAlignVertical.center,
                        decoration: InputDecoration(
                          focusedBorder: const OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0, color: Colors.transparent)),
                          disabledBorder: const OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0, color: Colors.transparent)),
                          enabledBorder: const OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0, color: Colors.transparent)),
                          contentPadding:
                          const EdgeInsets.symmetric(vertical: 0),
                          border: InputBorder.none,
                          labelStyle: TextStyles.textNormal28,
                          hintText: MultiLanguage.pleaseEnterNumberBinding.tr,
                          hintStyle: TextStyles.textSixthColor28,
                          suffixIcon:
                          asOrderController.keyWord.isNotEmpty
                              ? IconButton(
                            iconSize: 30.w,
                            icon: Icon(Icons.close,
                                color: Colours.gray_400, size: 40.w),
                            onPressed: () {
                              asOrderController.keyWorkSearchController.text = '';
                              asOrderController.keyWord.value = "";
                              asOrderController.update();
                            },
                          )
                              : gapZero,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(right: 24.w),
                    child: LocalImage('wms_scan',
                        package: Constant.packWms(), width: 32.w, height: 32.w),
                  ),
                ],
              )),
        ),
        textInkBgWell(MultiLanguage.search.tr, onTap: (action) {
          onSearch?.call(action);
        })
      ],
    ),
  );
}

Widget buildItem(
  WeighingGoodsChildEntity model, {
  Callback<WeighingGoodsChildEntity>? onTap,
  Callback<WeighingGoodsChildEntity>? onItemClick,
}) {
  List<Widget> widgets = [];

  widgets.add(gapH24);

  if(model.singleEntry){
    widgets.add(Padding(
        padding: edgeLR24,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text("${MultiLanguage.commodityCode.tr} : ", style: normalTextColor28),
            Expanded(child: Text(model.goodsBarCode, style: normalTextColor28)),
          ],
        )));
    widgets.add(gapH18);
  }

  List<Widget> widgetsA = [];

  widgetsA.add(Expanded(
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("${MultiLanguage.productName.tr} : ", style: normalTextColor28),
        Expanded(child: Text(model.goodsName, style: normalTextColor28)),
      ],
    ),
  ));

  if(model.singleEntry){
    widgetsA.add(Expanded(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text("${MultiLanguage.tareDeduction.tr}(${SPUtil.isJin.unitStrZh()}) : ", style: normalTextColor28),
          Expanded(
              child: Text(getWeightAsUnit(model.skinWeight).toString(), style: normalTextColor28)),
        ],
      ),
    ));
  }
  widgets.add(Padding(
      padding: edgeLR24,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: widgetsA,
      )));

  widgets.add(gapH18);

  widgets.add(Padding(
      padding: edgeLR24,
      child: Row(
        children: [
          Expanded(child: Row(children: [
            Text("${MultiLanguage.productBatch.tr} : ", style: normalTextColor28),
            Expanded(child: Text(model.batchNo, style: normalTextColor28)),
          ],)),
          Visibility(
            visible: Constant.isShowSeat,
            child: Expanded(child: Row(children: [
              Text("${MultiLanguage.seat.tr} : ", style: normalTextColor28),
              Expanded(child: Text(model.locationName, style: normalTextColor28)),
            ],)),
          ),
        ],
      )));
  widgets.add(gapH18);
  if(model.singleEntry){
    widgets.add(Padding(
        padding: edgeLR24,
        child: Row(
          children: [
            Expanded(child: Row(children: [
              Text("${MultiLanguage.mainCommodityWeightNoUnit.tr}(${SPUtil.isJin.unitStrZh()}) : ", style: getTextStyle(model.mainWeigh)),
              Expanded(child: Text("${getWeightAsUnit(model.mainWeigh)}", style: getTextStyle(model.mainWeigh))),
            ],)),
            Expanded(child: Row(children: [
              Text("${MultiLanguage.auxiliaryCommodityWeightNoUnit.tr}(${SPUtil.isJin.unitStrZh()}) : ", style: getTextStyle(model.mainWeigh)),
              Expanded(child: Text('${getWeightAsUnit(model.subWeigh)}', style: getTextStyle(model.subWeigh))),
            ],)),
          ],
        )));
  }else{
    widgets.add(Padding(
        padding: edgeLR24,
        child: Row(
          children: [
            Expanded(child: Row(children: [
              Text("${MultiLanguage.quantityAllottedNotN.tr} : ", style: getTextStyle(model.stockOutCount)),
              Expanded(child: Text('${model.stockOutCount}', style: getTextStyle(model.stockOutCount))),
            ],)),
            Expanded(child: Row(children: [
              Text("${MultiLanguage.distributionWeightNoUnit.tr}${SPUtil.isJin.unitStrZhWithBracket()} : ", style: getTextStyle(model.stockOutWeigh)),
              Expanded(child: Text(model.stockOutWeigh.numAsUnitStr(), style: getTextStyle(model.stockOutWeigh))),
            ],)),
          ],
        )));
  }
  widgets.add(gapH18);

  widgets.add(Padding(
      padding: edgeLR24,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text("${MultiLanguage.shortName.tr} : ", style: normalTextColor28),
          Expanded(child: Transform.translate(
              offset: const Offset(0, -2),
              child: Text(model.address, style: normalTextColor28,
                strutStyle: const StrutStyle(
                  forceStrutHeight: true,
                  height: 1,
                  leading: 0.3, //行距（0.5即行高的一半）
                ),
              ))),
        ],
      )));
  widgets.add(gapH18);
  return GestureDetector(
    onTap: () {
      onItemClick?.call(model);
    },
    child: roundWidget(
      margin: edgeLR24,
      decoration: rs8,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: widgets,
      ),
    ),
  );
}

double getWeightAsUnit(double weight){
  if(!SPUtil.isJin){
    return weight;
  }else{
    return weight*2;
  }
}

TextStyle getTextStyle(double weight){
  if(weight == 0){
    return eighthTextColor28;
  }else{
    return normalTextColor28;
  }
}


Widget buildBottom(
    StockOutChildDataEntity stockOutChildDataEntity,{
  void Function()? print,
  void Function()? deliverGoods,
}) {
  return Container(
    color: Colors.white,
    child: Column(
      children: [
        Container(
            height: 166.w,
            decoration: rectTop,
            alignment: Alignment.topRight,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(child: Container(
                  color: Colors.transparent,
                  child: Stack(
                    children: [
                      Positioned(
                        top: 47.w,
                        left: 24.w,
                        child: RichText(
                          text: TextSpan(
                              text: "${MultiLanguage.actualPick.tr}: ",
                              style: TextStyles.textNormal28,
                              children: [
                                TextSpan(text: getFinishedCount(stockOutChildDataEntity), style: TextStyles.primaryColor28),
                                TextSpan(text: '/${getTotalCount(stockOutChildDataEntity)}', style: TextStyles.textNormal28),
                              ]),
                        ),
                      ),
                    ],
                  ),
                )),
                GradientButton(
                  ThemeUtil.getTheme(),
                  decoration: BoxDecoration(
                      borderRadius: const BorderRadius.all(Radius.circular(20)),
                      border: Border.fromBorderSide(BorderSide(color: ThemeUtil.getTheme().sixthTextColor())),
                      color: ThemeUtil.getTheme().colorScheme.secondary),
                  height: 77.w,
                  width: 184.w,
                  margin: EdgeInsets.only(right: 16.w, top: 21.w),
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  text: MultiLanguage.print.tr,
                  onPressed: print,
                ),
                GradientButton(
                  ThemeUtil.getTheme(),
                  height: 77.w,
                  width: 184.w,
                  margin: EdgeInsets.only(right: 24.w, top: 21.w),
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  text: MultiLanguage.sendOut.tr,
                  onPressed:deliverGoods,
                ),
              ],
            )),
      ],
    ),
  );
}

String getFinishedCount(StockOutChildDataEntity stockOutChildDataEntity) {
  return stockOutChildDataEntity.finishedCount;
}

String getTotalCount(StockOutChildDataEntity stockOutChildDataEntity) {
  return stockOutChildDataEntity.totalCount;
}
